package com.itdct.onflow.generator.template

import com.itdct.onflow.generator.base.BaseTemplate
import com.itdct.onflow.generator.config.GeneratorConfig
import com.itdct.onflow.generator.constant.GroupEnum
import com.itdct.onflow.generator.model.ClassModel
import com.itdct.onflow.generator.model.CompareResultModel
import com.itdct.onflow.generator.model.FieldModel

/**
 * @author Zhouwx
 * @date 2025/8/11 16:45:17
 * @version 1.0
 * @description
 */
class SearchQoTemplate(classModel: ClassModel, generatorConfig: GeneratorConfig) : BaseTemplate(classModel, generatorConfig) {
    init {
        filterFieldList.add("id")
        filterFieldList.add("del")
        filterFieldList.add("version")
        filterFieldList.add("createUser")
        filterFieldList.add("updateUser")
        filterFieldList.add("deleteUser")
        filterFieldList.add("createTime")
        filterFieldList.add("updateTime")
        filterFieldList.add("deleteTime")
    }

    override fun render(): String {
        return """
${uuidPrint()}
package ${classModel.packageName}.qo

import com.itdct.onflow.core.qo.BasePageQo
import io.swagger.v3.oas.annotations.media.Schema
import java.util.*${extraImports()}

/**
 * @author ${generatorConfig.authorName}
 * @date ${getDateString()}
 * @version 1.0
 * @description ${classModel.classUsage}查询入参
 */
@Schema(name = "${classModel.classUsage}查询入参", description = "${classModel.classUsage}查询入参")
class ${classModel.className}SearchQo : BasePageQo() {
${rangeStart("fields")}
${setFields()}
${rangeEnd("fields")}
}
""".trimIndent()
    }

    override fun onModify(compareResultModel: CompareResultModel, content: String): String {
        var content = content
        val addFields = compareResultModel.addFields
        val addIndex = content.indexOf(rangeEnd("fields"))
        content = insertFieldListContent(content, addFields, addIndex, { index, fieldModel -> fieldContent(fieldModel) })

        val delFields = compareResultModel.delFields
        content = deleteRangeFunctionCode(content, "fields", delFields, { fieldModel -> fieldContent(fieldModel) })

        val modifyPairs = compareResultModel.modifyPairs
        content = modifyReplace(content, "fields", modifyPairs, { fieldModel -> fieldContent(fieldModel) })
        return content
    }

    private fun setFields(): String {
        return fieldsGen("", { i, fieldModel ->
            fieldContent(fieldModel)

        })
    }

    private fun fieldContent(fieldModel: FieldModel) = """
    |    @Schema(title = "${fieldModel.comment}", example = "")
    |    var ${fieldModel.name}: ${fieldModel.typeName}? = null
    |    
                """.trimMargin()

    private fun extraImports(): String {
        val stringBuilder = StringBuilder()
        classModel.fieldList.any {
            it.typeName == "BigDecimal"
        }.also {
            if (it) {
                stringBuilder.append("\nimport java.math.BigDecimal")
            }
        }
        return stringBuilder.toString()
    }

    override fun belongGroup(): GroupEnum {
        return GroupEnum.BACKEND
    }

    override fun subModuleName(): String {
        return "adaptor"
    }

    override fun getFolderFileName(): String {
        return "qo/${classModel.className}SearchQo.kt"
    }

}